﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class UC_CanBo_TDCT : UserControl
    {
        string temp = "";
        string temp1 = "";
        string flag = "";

        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public UC_CanBo_TDCT()
        {
            InitializeComponent();
        }

        private void btn_ThemTDCT_Click(object sender, EventArgs e)
        {
            frm_TDCT themtdct = new frm_TDCT();
            themtdct.ShowDialog();
        }

        private void UC_CanBo_TDCT_Load(object sender, EventArgs e)
        {
            Load_CB_TDCT();


            cmb_MaCB_TDCT.DataSource = sv.getCB_TDCT();
            cmb_MaCB_TDCT.DisplayMember = "Ma_CB";
            cmb_MaCB_TDCT.ValueMember = "Ma_CB";

            //Hiển thị tùy chọn trình độ chính trị
            cmb_STT_TDCT_TDCT.DataSource = sv.getTDCT();
            cmb_STT_TDCT_TDCT.DisplayMember = "TenTDCT";
            cmb_STT_TDCT_TDCT.ValueMember = "Stt_TD_CT";
        }
        public void Load_CB_TDCT()
        {
            dataGridView_TDCT.DataSource = sv.getCB_TDCT();
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
        }

        private void dataGridView_TDCT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cmb_MaCB_TDCT.Text = dataGridView_TDCT.CurrentRow.Cells[0].Value.ToString();
            cmb_STT_TDCT_TDCT.Text = dataGridView_TDCT.CurrentRow.Cells[1].Value.ToString();
            date_NamDT_TDCT.Text = dataGridView_TDCT.CurrentRow.Cells[2].Value.ToString();
            txt_TruongDT_TDCT.Text = dataGridView_TDCT.CurrentRow.Cells[3].Value.ToString();

            //Lấy vi ví cần xóa
            temp = dataGridView_TDCT.CurrentRow.Cells[0].Value.ToString();
            temp1 = dataGridView_TDCT.CurrentRow.Cells[1].Value.ToString();
            cmb_MaCB_TDCT.Enabled = false;
            cmb_STT_TDCT_TDCT.Enabled = false;
            date_NamDT_TDCT.Enabled = false;
            txt_TruongDT_TDCT.Enabled = false;
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag ="them";
            cmb_MaCB_TDCT.Enabled = true;
            cmb_STT_TDCT_TDCT.Enabled = true;
            date_NamDT_TDCT.Enabled = true;
            txt_TruongDT_TDCT.Enabled = true;
            cmb_MaCB_TDCT.Text = "";
            cmb_STT_TDCT_TDCT.Text = "";
            date_NamDT_TDCT.Text = "";
            txt_TruongDT_TDCT.Text = "";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            btb_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btb_Xoa.Enabled = true;
            btn_Sua.Enabled = true;

            ServiceReference1.CANBO_CHINHTRI cbtdct = new ServiceReference1.CANBO_CHINHTRI();
            string a = cmb_MaCB_TDCT.Text;
            string b = cmb_STT_TDCT_TDCT.SelectedValue.ToString();
            string c = date_NamDT_TDCT.Text;
            string d = txt_TruongDT_TDCT.Text;

            cbtdct.Ma_CB = a;
            cbtdct.Stt_TD_CT = Convert.ToInt32(b);
            cbtdct.NamDT = Convert.ToDateTime(c);
            cbtdct.TruongDT = d;

            if (a == "" | b == "" | c == "" | d == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.checkCB_TDCT(cbtdct))
                        kq = -1;
                    else
                        sv.addCB_TDCT(cbtdct);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setCB_TDCT(cbtdct);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_CB_TDCT();
                    btb_Xoa.Enabled = true;
                    btn_Sua.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btb_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
        }

        private void btb_Xoa_Click(object sender, EventArgs e)
        {
            ServiceReference1.CANBO_CHINHTRI cbtdct = new ServiceReference1.CANBO_CHINHTRI();
            cbtdct.Ma_CB = temp;
            cbtdct.Stt_TD_CT = Convert.ToInt32(temp1);
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteCB_TDCT(cbtdct);
                Load_CB_TDCT();
                MessageBox.Show("Đã xóa!");
                cmb_MaCB_TDCT.Text = "";
                cmb_STT_TDCT_TDCT.Text = "";
                date_NamDT_TDCT.Text = "";
                txt_TruongDT_TDCT.Text = "";
            }

        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag ="sua";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            date_NamDT_TDCT.Enabled = true;
            txt_TruongDT_TDCT.Enabled = true;
        }
    }
}
